import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import 'state.dart';

class InvoiceHeaderManagementLogic extends GetxController {
  final state = InvoiceHeaderManagementState();
  @override
  void onInit() {
    super.onInit();

    if (Get.arguments != null) {
      state.chooseBool = Get.arguments['chooseType'] ?? false;
    } else {
      state.chooseBool = false;
    }
  }
  @override
  void onReady() {
    super.onReady();

    var params = {
      'use_platform': 2,
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };
    Api.commonInvoiceHeader(params).listen((event) {
      print('+++++++++**************  成功  常用发票抬头  event = $event');
      if (event['code'] == 0) {
        List list = event['invoice_title_infos'];
        if (list.length > 0) {
          state.commonInvoiceHeader = list;
        }
      }
      update();
    }, onError: (error) {
      print('+++++++++**************  失败  常用发票抬头  error = $error');
    });

  }



  void deleteInvoiceHeader(int index) {
    var params = {
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'invoice_title_num_id': state.commonInvoiceHeader[index]['invoice_title_num_id'],
    };
    Api.deleteInvoiceHeader(params).listen((event) {
      print('-+-+-+-+-+-+-+-+-+-+-+- 删除成功！     event = $event');
      if (event['code'] == 0) {
        state.commonInvoiceHeader.removeAt(index);
      } else {
        toast('删除失败');
      }
      update();
    }, onError: (error) {
      print('-+-+-+-+-+-+-+-+-+-+-+- 删除失败！     error = $error');
    });
  }
}